﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.WebCommon.Common;
using SMTC.BO.Line;
using SMTC.Model.Domain;
using SMTC.Model.Model;
public partial class BaseInfoManagement_ModifyLineInfo : BasePage
{
    LineProccessServices proc = new LineProccessServices();
    LineQueryServices query = new LineQueryServices();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request["action"] != null)
            {
                ViewState["action"] = Request["action"].ToString();
                if (ViewState["action"].ToString().Equals("add"))
                {
                    this.lbtitle.Text = Resources.Resource.LineInfoAdd;//"生产线信息添加";

                }
                else
                {
                    if (Request["lineID"] != null)
                    {
                        ViewState["lineID"] = Request["lineID"].ToString();
                    }
                    this.lbtitle.Text = Resources.Resource.LineInfoMangement;//"生产线信息维护";

                    try
                    {
                        LineDomain domain = query.GetlineDomain(ViewState["lineID"].ToString());
                        this.txtLineName.Text = domain.line.LineName;
                        this.txtLineTargetTime.Text = domain.line.LineTargetTime.ToString();
                        List<SMT_ProductionUnitModel> list = domain.puList;

                        foreach (SMT_ProductionUnitModel item in list)
                        {
                            if (item != null)
                            {
                                ListItem li = new ListItem(item.PUType, item.PUID + "|" + item.PUCapacity + "|" + item.Para);
                                this.lbAddedPU.Items.Add(li);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessage(Resources.Resource.ErrorForGetMounterList);//"取得贴片机列表失败！请联系管理员"
                        return;
                    }
                    lbAddedPU.DataBind();
                }
            }
        }
    }


    protected void btnUpd_OnClick(object sender, EventArgs e)
    {

        LineDomain domain = new LineDomain();
        SMT_LineModel line = new SMT_LineModel();
        line.LineName = this.txtLineName.Text;
        line.LineTargetTime = Convert.ToDouble(txtLineTargetTime.Text);
        line.ModifyDateTime = DateTime.Now;
        line.ModifyPerson = this.UserName;
        line.LineID = ViewState["lineID"] == null ? Guid.NewGuid().ToString() : ViewState["lineID"].ToString();
        domain.line = line;
        List<SMT_ProductionUnitModel> pulist = new List<SMT_ProductionUnitModel>();
        foreach (ListItem item in lbAddedPU.Items)
        {
            string[] str = item.Value.Split('|');
            SMT_ProductionUnitModel pu = new SMT_ProductionUnitModel();
            pu.LineID = line.LineID;
            pu.PUID = str[0];
            pu.PUType = item.Text;
            pu.PUCapacity = Convert.ToInt32(str[1]);
            pu.Para = Convert.ToDouble(str[2]);
            pulist.Add(pu);
        }
        domain.puList = pulist;

        if (ViewState["action"].ToString().Equals("add"))
        {

            if (proc.InsertLine(domain))
            {
                this.ShowMessage(Resources.Resource.LineInfoAddSuccess);//"生产线信息添加成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.LineInfoAddFailed);//"生产线信息添加失败！请稍后重试！"
            }
        }
        else
        {
            if (proc.UpdateLine(domain))
            {
                this.ShowMessage(Resources.Resource.LineInfoUpdSuccess);//"生产线信息维护成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.LineInfoUpdFailed);//"生产线信息维护失败！请稍后重试！"
            }
        }
        this.btnUpd.Visible = false;

    }

    protected void btnAddPU_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.HidAddedPU.Value) && this.HidAddedPU.Value.ToLower() != "undefined")
        {
            string[] pu = this.HidAddedPU.Value.Split(';');

            if (pu.Length == 0) return;
            for (int i = 0; i < pu.Length; i++)
            {
                bool flag = true;

                for (int j = 0; j < lbAddedPU.Items.Count; j++)
                {

                    if (lbAddedPU.Items[j].Value == pu[i].Split(',')[0].ToString() + "|" + pu[i].Split(',')[2].ToString() + "|" + pu[i].Split(',')[3].ToString())
                    {
                        flag = false;

                    }

                }
                if (flag)
                {
                    lbAddedPU.Items.Add(new ListItem(pu[i].Split(',')[1].ToString(), pu[i].Split(',')[0].ToString() + "|" + pu[i].Split(',')[2].ToString() + "|" + pu[i].Split(',')[3].ToString()));

                }

            }
        }



    }
    protected void btnRemovePU_Click(object sender, EventArgs e)
    {

        while (lbAddedPU.SelectedIndex != -1)
        {
            lbAddedPU.Items.Remove(this.lbAddedPU.SelectedItem);
        }
        if (lbAddedPU.Items.Count == 0)
        {
            this.HidAddedPU.Value = "";
        }

    }

}